package com.hnnu.shoppingtoghter.demo.controller;

import com.hnnu.shoppingtoghter.demo.entity.SysUser;
import com.hnnu.shoppingtoghter.demo.service.SysUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Controller;

import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PutMapping;

import org.springframework.web.bind.annotation.RequestMapping;
import com.hnnu.shoppingtoghter.demo.entity.Project;
import com.hnnu.shoppingtoghter.demo.service.ProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.List;

/**
 * @Description:
 * @Author: 你的名字
 * @CreateTime： 2022/6/28 15:18
 */
@Slf4j
@Controller
public class UserController {
    @Autowired
    private SysUserService userService;
    @Autowired
    private ProjectService projectService;
    @RequestMapping("/my_index")
    public String Myindex() {
        return "my_index";
    }

    //商品详情页跳转：刘思宇
//    @RequestMapping("/project")
//    public String project(Project project,Model model) {
//
//
//        return "project";
//    }

    /**
     * @Description: 公司介绍
     * @Author: Liu_aihuan
     * @CreateTime: 2022/6/29 17:33
     * @return: java.lang.String
     **/
    @RequestMapping("/ourintroduce")
    public String Ourintroduce(Project project,Model model) {

        model.addAttribute("project1", project);
        System.out.println("project1 =" + project);
        List<Project> projectList1 = projectService.selectProjectByUser(project);
        System.out.println("projectList1 =" + projectList1);
        model.addAttribute("projectList1", projectList1);



        return "ourintroduce";
    }
    //购物车跳转：孔林
    @GetMapping("/shop_car")
    public String shop_car(){
//        Object object = session.getAttribute("user");
//        if(null == object){
//            return "redirect/login";
//        }
        return "shop_car";
    }


    /**
     * @Description: 结算页面
     * @Author: kong_lin
     * @CreateTime:  11:05
     * @param: session
     * @return: java.lang.String
     **/
    @GetMapping("/confirm_order")
    public String confirm_order(){
        return "confirm_order";
    }

    /**
     * @Description: 添加收货地址
     * @Author: kong_lin
     * @CreateTime:  14:41
     * @param: session
     * @return: java.lang.String
     **/

//    @GetMapping("/my_contact")
//    public String my_contact(){
//        return "my_contact";
//    }
//


    /**
     * @Description: 显示我的信息
     * @Author: Liu_aihuan
     * @CreateTime: 2022/7/5 23:56
     * @param: null
     * @return: null
     **/
    @GetMapping("/my_info")
    public String updateIndex(Model model, HttpSession session){

        return "my_info";
    }

    /**
     * @Description: 编辑我的信息
     * @Author: Liu_aihuan
     * @CreateTime: 2022/7/6 1:17
     * @param: null
     * @return: null
     **/
    @PutMapping("/my_info")
    public String update(Model model, SysUser user, HttpSession session){
        /** 获取登录用户信息*/
        SysUser loginUser = (SysUser) session.getAttribute("user");

        log.info("修改前的用户信息：{}", loginUser);
        log.info("修改后的用户信息：{}", user);
        user.setId(loginUser.getId());
        user.setUserCode(loginUser.getUserCode());
        user.setPassword(loginUser.getPassword());
        user.setPhone(loginUser.getPhone());
        user.setUserRole(loginUser.getUserRole());
        user.setHeadImages(loginUser.getHeadImages());
        log.info("session中未设置修改的数据被set后的用户信息：{}", user);
//        user.setId(loginUser.getId());
//        user.setId(loginUser.getId());
        int rows = userService.updateUser(user);
        if (rows == 0){
            System.out.println("用户信息更新失败！");
            return "my_info";
        }
        BeanUtils.copyProperties(user, loginUser);
        session.setAttribute("user", loginUser);
        return "redirect:/my_info";
    }
    
    /**
     * @Description: 显示修改密码页面
     * @Author: Liu_aihuan
     * @CreateTime: 2022/7/6 2:34
     * @param: null 
     * @return: null
     **/
    @GetMapping("/security")
    public String updateIndexPassword(Model model, HttpSession session) {

        return "security";
    }


    /**
     * @Description: 编辑修改密码页面
     * @Author: Liu_aihuan
     * @CreateTime: 2022/7/6 2:37
     * @param: model
     * @param: session
     * @return: java.lang.String
     **/
    @PutMapping("/security")
    public String updatePassword(Model model, SysUser user, HttpSession session){
        /** 获取登录用户信息*/
        SysUser loginUser = (SysUser) session.getAttribute("user");

        log.info("修改前的用户信息：{}", loginUser);
        log.info("修改后的用户信息：{}", user);
        user.setId(loginUser.getId());
        user.setUserCode(loginUser.getUserCode());
        user.setUserName(loginUser.getUserName());
        user.setGender(loginUser.getGender());
        user.setBirthday(loginUser.getBirthday());
        user.setPhone(loginUser.getPhone());
        user.setAddress(loginUser.getAddress());
        user.setUserRole(loginUser.getUserRole());
        user.setHeadImages(loginUser.getHeadImages());
        log.info("session中未设置修改的数据被set后的用户信息：{}", user);
        if (user.getOldpassword().equals(loginUser.getPassword())){
            log.info("旧密码正确：{}");
            if (user.getRuserpassword().equals(user.getPassword())){
                log.info("再次输入新密码正确：{}");
                int rows = userService.updateUserPassword(user);
                if (rows == 0){
                    System.out.println("用户信息更新失败！");
                    return "security";
                }
                log.info("密码修改成功：{}");
                /** 将session中的登录用户信息更新*/
                BeanUtils.copyProperties(user, loginUser);
                session.setAttribute("user", loginUser);
                return "security";

            }else {
                log.info("再次输入新密码不正确：{}");
                return "security";
            }

        }else {
            log.info("旧密码不正确：{}");
            return "security";
        }

    }





}